<template>
    <el-container>
        <el-header style="margin-top: 5px;">
            <h2>爬虫任务代码展示</h2>
        </el-header>

        <el-main>
            <!-- 代码展示区域 -->
            <el-card shadow="always" class="code-card" style="margin-top: -20px;">
                <pre class="code-block">
  def download_image(image_url):
      # 确保静态图片目录存在，如果不存在则创建
      img_dir = os.path.join('static', 'img')
      if not os.path.exists(img_dir):
          os.makedirs(img_dir)
  
      # 获取图片文件的扩展名
      file_name = str(uuid.uuid4()) + os.path.splitext(urlparse(image_url).path)[-1]
      file_path = os.path.join(img_dir, file_name)
  
      try:
          # 下载图片
          img_data = requests.get(image_url).content
          logging.error("图片数据:", img_data)
          with open(file_path, 'wb') as f:
              f.write(img_data)
          return file_path  # 返回保存的图片路径
      except Exception as e:
          print(f"Error downloading image: {e}")
          return None
  
  def saveDataToDB(movieList):
      # 保存电影数据到数据库并下载图片
      conn, cursor = connect_to_database()
  
      for movie in movieList:
          try:
              # 下载图片并获取本地路径
              img_local_path = download_image(movie['img_link'])
              if img_local_path:
                  # 更新电影字典中的图片路径
                  movie['img_link'] = img_local_path
  
              # 保存电影信息到数据库
              cursor.execute("""
                  INSERT INTO movies (title, url, img_link, star, quote, number, year, people)
                  VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
              """, (movie['title'], movie['url'], movie['img_link'], movie['star'], movie['quote'], movie['number'],
                    movie['year'], movie['people']))
  
              conn.commit()  # 提交事务
          except Exception as e:
              print(f"Error saving movie: {e}")
              conn.rollback()  # 出现错误时回滚
  
      cursor.close()
      conn.close()
  
  def getEveryItem(source):
      # 爬取每一页电影数据
      html_element = etree.HTML(source)
      movieItemList = html_element.xpath('//div[@class="item"]')
  
      movieList = []
      for eachMovie in movieItemList:
          movieDict = {}
  
          title = eachMovie.xpath('div[@class="info"]/div[@class="hd"]/a/span[@class="title"]/text()')[0]
          link = eachMovie.xpath('div[@class="info"]/div[@class="hd"]/a/@href')[0]
          img_link = eachMovie.xpath('div[@class="pic"]/a/img//@src')[0]
          star = eachMovie.xpath('div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[@class="rating_num"]/text()')[0]
          quote = eachMovie.xpath('div[@class="info"]/div[@class="bd"]/p[@class="quote"]/span/text()')
          number = eachMovie.xpath('div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[4]/text()')[0]
          year = eachMovie.xpath('div[@class="info"]/div[@class="bd"]/p[1]/text()')[1]
          people = eachMovie.xpath('div[@class="info"]/div[@class="bd"]/p[1]/text()')[0]
  
          if quote:
              quote = quote[0]
          else:
              quote = ''
  
          movieDict['title'] = ''.join(title)
          movieDict['url'] = link
          movieDict['img_link'] = img_link
          movieDict['star'] = star
          movieDict['quote'] = quote
          movieDict['number'] = number
          movieDict['year'] = year
          movieDict['people'] = people
  
          movieList.append(movieDict)
  
      return movieList
  
  @app.route('/spider', methods=['POST'])
  def spider():
      # 路由处理爬虫任务
      doubanUrl = 'https://movie.douban.com/top250?start={}&filter='
      headers = {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
      }
  
      movieList = []
      for i in range(2):  # 爬取10页
          print(f'正在爬取第{i + 1}页')
          pageLink = doubanUrl.format(i * 25)
          res = requests.get(pageLink, headers=headers)
          res.encoding = 'utf-8'
          source = res.text
          movieList += getEveryItem(source)
          time.sleep(2)  # 每爬取一页停顿2秒，避免请求过于频繁
  
      saveDataToDB(movieList)  # 将爬取到的数据保存到数据库
  
      return jsonify({'message': "爬取成功"}), 200
            </pre>
            </el-card> </el-main>

        <el-footer>
            <el-alert title="提示：请确保运行环境正确配置。" type="info" show-icon>
            </el-alert>
        </el-footer>
    </el-container>
</template>

<script>
</script>

<style scoped>
    .code-card {
        margin: 20px 0;
        padding: 10px;
        background-color: #f5f5f5;
        border-radius: 8px;
    }

    .code-block {
        font-family: "Courier New", Courier, monospace;
        font-size: 14px;
        color: #333;
        white-space: pre-wrap;
        line-height: 1.5;
    }

    .button-group {
        display: flex;
        justify-content: space-between;
        margin-top: 20px;
    }
</style>